package com.czn.controller.admin;

import com.czn.entity.Medicalhistory;
import com.czn.service.IDoctorService;
import com.czn.service.IMedicalHistoryService;
import com.czn.service.IPatientService;
import com.czn.vo.QueryVo;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 病史管理控制器
 *
 * @author ChenZeNan
 * @create 2021-04-23 15:53
 */
@Controller
@RequestMapping("/admin/medicalHistory")
public class AdminMedicalHistoryController {
    @Autowired
    private IMedicalHistoryService medicalHistoryService;
    @Autowired
    private IPatientService patientService;
    @Autowired
    private IDoctorService doctorService;

    /**
     * 分页/模糊查询
     *
     * @param queryVo
     * @param model
     * @return
     */
    @RequestMapping("/manage")
    public String medicalHistoryManage(QueryVo queryVo, Model model) {
        PageQuery<Medicalhistory> page = medicalHistoryService.findMedicalHistoryList(queryVo);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", queryVo.getPageNo());
        model.addAttribute("patientName", queryVo.getPatientName());
        model.addAttribute("doctorName", queryVo.getDoctorName());
        model.addAttribute("path", "/admin/medicalHistory/manage");
        StringBuilder sb = new StringBuilder();
        if (queryVo.getPatientName() != null) {
            sb.append("&patientName=" + queryVo.getPatientName());
        }
        if (queryVo.getDoctorName() != null) {
            sb.append("&doctorName=" + queryVo.getDoctorName());
        }
        model.addAttribute("params", sb.toString());
        return "admin/medicalHistoryManage";
    }

    /**
     * 跳转到新增病史页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String toAddFormPage(Model model) {
        model.addAttribute("medicalHistory", new Medicalhistory());
        model.addAttribute("patients", patientService.findAllPatient());
        model.addAttribute("doctors", doctorService.findAllDoctor());
        return "admin/medicalHistoryForm";
    }

    /**
     * 跳转到病史修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String toEditFormPage(@PathVariable Integer id, Model model) {
        Medicalhistory medicalhistory = medicalHistoryService.findMedicalHistoryById(id);
        model.addAttribute("medicalHistory", medicalhistory);
        model.addAttribute("patients", patientService.findAllPatient());
        model.addAttribute("doctors", doctorService.findAllDoctor());
        return "admin/medicalHistoryForm";
    }

    /**
     * 新增病史
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Medicalhistory medicalhistory) {
        ResponseResult result = medicalHistoryService.addMedicalHistory(medicalhistory);
        return result;
    }

    /**
     * 编辑病史
     *
     * @param medicalhistory
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult edit(@RequestBody Medicalhistory medicalhistory) {
        ResponseResult result = medicalHistoryService.editMedicalHistory(medicalhistory);
        return result;
    }

    /**
     * 删除病史
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable Integer id) {
        ResponseResult result = medicalHistoryService.deleteMedicalHistoryById(id);
        return result;
    }


}
